home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / ftp.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  5KB  |  212 lines

  1. /* Stuff common to both the FTP server and client */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "timer.h"
  8. #include "tcp.h"
  9. #include "ftp.h"
  10. #include "telnet.h"
  11. #include "iface.h"
  12. #include "ax25.h"
  13. #include "lapb.h"
  14. #include "finger.h"
  15. #include "session.h"
  16. #ifdef    SYS5
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #ifdef    hp9000s500
  20. #include <sys/param.h>
  21. #endif    /* hp9000s500 */
  22. #ifdef    SUNOS4
  23. #define    IFIFO    S_IFIFO
  24. #else    /* SUNOS4 */
  25. #include <sys/inode.h>
  26. #endif    /* SUNOS4 */
  27. #endif    /* SYS5 */
  28. #include "nr4.h"
  29.  
  30. #if    (ATARI_ST && MWC)
  31. #define    fclose    vclose        /* Take care of temp files -- hyc */
  32. #endif
  33.  
  34. /* FTP Data channel Receive upcall handler */
  35. void
  36. ftpdr(tcb,cnt)
  37. struct tcb *tcb;
  38. int16 cnt;
  39. {
  40.     register struct ftp *ftp;
  41.     struct mbuf *bp;
  42.     char c;
  43.  
  44.     ftp = (struct ftp *)tcb->user;
  45.     if(ftp->state != RECEIVING_STATE){
  46.         close_tcp(tcb);
  47.         return;
  48.     }
  49.     /* This will likely also generate an ACK with window rotation */
  50.     recv_tcp(tcb,&bp,cnt);
  51.  
  52. #if (UNIX || MAC || AMIGA || ATARI_ST)
  53.     if(ftp->type == ASCII_TYPE){
  54.         while(pullup(&bp,&c,1) == 1){
  55.             if(c != '\r')
  56.                 putc(c,ftp->fp);
  57.         }
  58.         return;
  59.     }
  60. #endif
  61.     while(bp != NULLBUF){
  62.         if(bp->cnt != 0)
  63.             fwrite(bp->data,1,(unsigned)bp->cnt,ftp->fp);
  64.         bp = free_mbuf(bp);
  65.     }
  66.  
  67.      if(ftp->fp != stdout && ferror(ftp->fp)){ /* write error (dsk full?) */
  68.          fclose(ftp->fp);
  69.          ftp->fp = NULLFILE;
  70.          close_self(tcb,RESET);
  71.      }
  72. }
  73. /* FTP Data channel Transmit upcall handler */
  74. void
  75. ftpdt(tcb,cnt)
  76. struct tcb *tcb;
  77. int16 cnt;
  78. {
  79.     struct ftp *ftp;
  80.     struct mbuf *bp;
  81.     register char *cp;
  82.     register int c;
  83.     int eof_flag;
  84. #ifdef    SYS5
  85.     struct stat ss_buf;
  86. #endif
  87.  
  88.     ftp = (struct ftp *)tcb->user;
  89.     if(ftp->state != SENDING_STATE){
  90.         close_tcp(tcb);
  91.         return;
  92.     }
  93.     if((bp = alloc_mbuf(cnt)) == NULLBUF){
  94.         /* Hard to know what to do here */
  95.         return;
  96.     }
  97.     eof_flag = 0;
  98.     if(ftp->type == IMAGE_TYPE){
  99.         bp->cnt = fread(bp->data,1,cnt,ftp->fp);
  100.         if(bp->cnt != cnt)
  101.             eof_flag = 1;
  102.     } else {
  103.         cp = bp->data;
  104.         while(cnt > 1){
  105.             if((c = getc(ftp->fp)) == EOF){
  106.                 eof_flag=1;
  107.                 break;
  108.             }
  109. #if (defined(CPM) || defined(MSDOS))
  110.             /* ^Z is CP/M's text EOF marker, and it is sometimes used
  111.              * by MS-DOS editors too
  112.              */
  113.             if(c == CTLZ){
  114.                 eof_flag = 1;
  115.                 break;
  116.             }
  117. #endif
  118. #if (defined(UNIX) || defined(MAC) || defined(AMIGA) || defined(ATARI_ST))
  119.             if(c == '\n'){
  120.                 *cp++ = '\r';
  121.                 bp->cnt++;
  122.                 cnt--;
  123.             }
  124. #endif
  125.             *cp++ = c;
  126.             bp->cnt++;
  127.             cnt--;
  128.         }
  129.     }
  130.     if(bp->cnt != 0)
  131.         send_tcp(tcb,bp);
  132.     else
  133.         free_p(bp);
  134.  
  135.     if(eof_flag){    /* EOF seen */
  136. #ifdef    UNIX
  137. #ifdef    SYS5
  138. #ifndef    hp9000s500
  139. /* If ftp->fp points to an open pipe (from dir()) it must be closed with */
  140. /* pclose().  System V fstat() can tell us if this was a pipe or not. */
  141.         if (fstat(fileno(ftp->fp), &ss_buf) < 0)
  142.             perror("ftpdt: fstat");
  143.         if ((ss_buf.st_mode & IFIFO) == IFIFO)
  144.             pclose(ftp->fp);    /* close pipe from dir */
  145.         else
  146.             fclose(ftp->fp);
  147. #else    /* hp9000s500 */
  148. /* HP-UX 5.21 on the 500 doesn't understand IFIFO, since we probably don't *.
  149. /* care anyway, treat it like BSD is treated... */
  150.         if (pclose(ftp->fp) < 0)
  151.             fclose(ftp->fp);
  152. #endif    /* hp9000s500 */
  153. #else    /* SYS5 */
  154. /* Berkeley Unix can't tell if this was a pipe or not.  Try a pclose() */
  155. /* first.  If this fails, it must have been an open file. */
  156.         if (pclose(ftp->fp) < 0)
  157.             fclose(ftp->fp);
  158. #endif    /* SYS5 */
  159. #else    /* UNIX */
  160. /* Anything other than Unix */
  161.         fclose(ftp->fp);
  162. #endif    /* UNIX */
  163.         ftp->fp = NULLFILE;
  164.         close_tcp(tcb);
  165.     }
  166. }
  167. /* Allocate an FTP control block */
  168. struct ftp *
  169. ftp_create(bufsize)
  170. unsigned bufsize;
  171. {
  172.     void ftp_delete();
  173.     register struct ftp *ftp;
  174.  
  175.     if((ftp = (struct ftp *)calloc(1,sizeof (struct ftp))) == NULLFTP)
  176.         return NULLFTP;
  177.     if(bufsize != 0 && (ftp->buf = malloc(bufsize)) == NULLCHAR){
  178.         printf("called by ftp_create\n");fflush(stdout);
  179.         ftp_delete(ftp);
  180.         printf("called by ftp_create\n");fflush(stdout);
  181.         return NULLFTP;
  182.     }
  183.     ftp->state = COMMAND_STATE;
  184.     ftp->type = ASCII_TYPE;    /* Default transfer type */
  185.     return ftp;
  186. }
  187. /* Free resources, delete control block */
  188. void
  189. ftp_delete(ftp)
  190. register struct ftp *ftp;
  191. {
  192.         int i;
  193.  
  194.     if(ftp->fp != NULLFILE && ftp->fp != stdout)
  195.         fclose(ftp->fp);
  196.     if(ftp->data != NULLTCB)
  197.         del_tcp(ftp->data);
  198.     if(ftp->username != NULLCHAR)
  199.         free(ftp->username);
  200.         for (i = 0; i < MAXPATH; i++)
  201.       if(ftp->path[i] != NULLCHAR)
  202.         free(ftp->path[i]);
  203.     if(ftp->buf != NULLCHAR)
  204.         free(ftp->buf);
  205.     if(ftp->cd != NULLCHAR)
  206.         free(ftp->cd);
  207.     if(ftp->session != NULLSESSION)
  208.         freesession(ftp->session);
  209.     free((char *)ftp);
  210. }
  211.  
  212.